package com.fastloan.safe.mgt.sys.service.impl;

import com.fastloan.safe.mgt.sys.model.CmuSysFile;
import com.fastloan.safe.mgt.sys.service.CmSysFileService;
import com.fastloan.safe.mgt.sys.service.IWebFileService;
import com.kawins.supers.file.core.FileLoadBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author rankai
 *         createTime 2017-10-2017/10/27 18:15
 */
@Service
public class IWebFileServiceImpl implements IWebFileService {

    @Autowired
    private CmSysFileService sysFileService;

    @Override
    public Object insertFile(FileLoadBean loadBean) {
        CmuSysFile cmuSysFile = new CmuSysFile();
        cmuSysFile.setCreateTime(new Date()).setFileName(loadBean.getFileName())
                .setFileLength(String.valueOf(loadBean.getFileLength())).setFileExt(loadBean.getFileExt())
                .setFileServer(loadBean.getFileServer()).setGroupName(loadBean.getGroupName())
                .setRemoteFileName(loadBean.getRemoteFileName());
        sysFileService.insertSelective(cmuSysFile);
        return cmuSysFile.getId();
    }

    @Override
    public FileLoadBean selectFile(Object id) {
        CmuSysFile cmuSysFile = sysFileService.selectByPrimaryKey(id);
        FileLoadBean loadBean = null;
        if (cmuSysFile != null) {
            loadBean = new FileLoadBean(cmuSysFile.getFileServer(), cmuSysFile.getGroupName(), cmuSysFile.getRemoteFileName());
            loadBean.setFileId(cmuSysFile.getId());
            loadBean.setFileExt(cmuSysFile.getFileExt());
            loadBean.setFileLength(Long.parseLong(cmuSysFile.getFileLength()));
            loadBean.setFileName(cmuSysFile.getFileName());
        }
        return loadBean;
    }

    @Override
    public void deleteFile(Object id) {
        sysFileService.deleteByPrimaryKey(id);
    }
}
